package tw.edu.tku.im.maze;
import org.json.JSONException;
import org.json.JSONObject;


public class RegisterRepParser extends PacketParser {
   public static RegisterRepParser INSTANCE = new RegisterRepParser();
   
   private RegisterRepParser() { }

   @Override
   public RegisterRep parse(JSONObject jsonObj) throws JSONException {
      JSONObject result = jsonObj.getJSONObject("result");
      int state = result.getInt("state");
      if(result.isNull("playerInfo")) {
         return new RegisterRep(state);
      } 
      else {
         JSONObject playerInfo = result.getJSONObject("playerInfo");
         String userID = playerInfo.getString("userID");
         String name = playerInfo.getString("name");
         int ticket = playerInfo.getInt("ticket");
         return new RegisterRep(state, new PlayerInfo(userID, name, ticket));
      }
   }

   @Override
   public String acceptName() {
      return "registerResponse";
   }
   
}
